var express = require('express');
var router = express.Router();
// var request = require('./utils/request')
/* GET home page. */

router.get('/', function (req, res, next) {
    let response = ''
    const id = req.query.id
    console.log(id)
    if (!id) {
        Movie.fetch()
            .then(doc => {
                response = createResult('查询成功', doc)
                res.json(response);
            })
            .catch(err => {
                response = createResult('未找到相关结果', [])
                res.json(response);
            })
    } else {
        Movie.find({
            _id: id
        }).then(doc => {
            console.log('成功')
            if(doc.length) response = createResult('查询成功', ...doc)
            else response = createResult('未找到相关结果',[])
        }).catch(err => {
            console.log('失败')
            response = createResult('未找到相关结果', [])
        }).then(() => {
            res.json(response);
        })
    }
});

module.exports = router;